package com.minyifang.wcspf.work.ui.user.presenter;

import com.google.gson.Gson;
import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.user.model.RefundBean;
import com.minyifang.wcspf.work.ui.user.model.UploadPicBean;
import com.minyifang.wcspf.work.ui.user.view.ApplyRefundView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/8/7.
 */
public class ApplyRefundPresenter extends BasePresenter<ApplyRefundView> {
    public void getData(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getRefoundData(requestBody), respond -> {
            if (respond.result == 1) {
                LogUtil.e("退单数据：" + SafeUtils.decrypt(respond.data));
                RefundBean bean = new Gson().fromJson(SafeUtils.decrypt(respond.data), RefundBean.class);
                getView().showRefundRes(bean);
            } else
                getView().showRequestFailed(respond.message);
        });
    }

    /**
     * 勾选退钱
     *
     * @param orderId
     * @param comId
     */
    public void calculateMoney(String orderId, String comId) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("comId", comId);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().calculateRefoundMoney(requestBody), respond -> {
            if (respond.result == 1) {
                LogUtil.e("退单算钱：" + SafeUtils.decrypt(respond.data));
                getView().showCalculateRes(SafeUtils.decrypt(respond.data));
            } else
                getView().showRequestFailed(respond.message);
        });
    }

    public void updateHeadImg(File imgFile) {
        // 创建RequestBody，传入参数："multipart/form-data"，String
        RequestBody requestToken = RequestBody.create(MediaType.parse("multipart/form-data"), UserManager.getInstance().getToken());
        RequestBody requestClient = RequestBody.create(MediaType.parse("multipart/form-data"), "android");
        // 创建RequestBody，传入参数："multipart/form-data"，File
        RequestBody requestImgFile = RequestBody.create(MediaType.parse("multipart/form-data"), imgFile);
        // 创建MultipartBody.Part，用于封装文件数据
        MultipartBody.Part requestImgPart = MultipartBody.Part.createFormData("files", imgFile.getName(), requestImgFile);
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().uploadImage(requestToken, requestClient, requestImgPart), listHttpRespond -> {
            getView().hideLoadingView();
            if (listHttpRespond.result == 1) {
                getView().onUploadPicSucceed(listHttpRespond.data);
            }
        });
    }

    public void commitRefundInfo(String orderId, JSONArray array, String pic, String reason) {
        getView().showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("comId", array);
        map.put("pic", pic);
        map.put("des", reason);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().commitRefundInfo(requestBody), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1) {
                getView().showCommitRefundInfoRes(respond.message);
            } else
                getView().showRequestFailed(respond.message);
        });
    }
}
